﻿package plugin.everyday
{
    import cmd.*;
    import com.core.*;
    import com.sh.game.util.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.utils.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.instance.*;
    import uiresource.everyday.*;
    import view.*;
    import view.message.*;
    import view.mornExtends.*;

    public class EveryDayCon extends EverydayUI
    {
        private var _instance:EveryDay;
        private var gameactive:Object;
        private var sp2:Sprite;
        private var isflag:Boolean = true;
        private var listarr:Array;
        private var hefu:Boolean;
        private var hefuDay:int;
        private var cfgtools:Object;
        private var arr3:Array;
        private var _curdata:Object;
        private var _selectIndex:int;
        private var _timeIntvalId:uint;
        private var blueline:Image;
        private static const START:String = "未开启";
        private static const END:String = "已结束";
        private static const GOING:String = "前往";

        public function EveryDayCon(param1:EveryDay)
        {
            this.sp2 = new Sprite();
            this.arr3 = [];
            this._instance = param1;
            return;
        }// end function

        override protected function initializeView() : void
        {
            super.initializeView();
            var _loc_1:* = Control.create(EveryDayCtrl, this);
            ModelProxy.addObserver("everyday", _loc_1);
            btn_close.addEventListener(MouseEvent.CLICK, this.onclose);
            label_shop.text = "攻击药水";
            label_shop.addEventListener(MouseEvent.CLICK, this.gotoBuy);
            label_shop.addEventListener(MouseEvent.MOUSE_OVER, this.onOver);
            label_shop.addEventListener(MouseEvent.MOUSE_OUT, this.mouseOut);
            this.dshdView();
            return;
        }// end function

        public function openRefresh() : void
        {
            this.rightChange(0);
            if (this.isflag == false)
            {
                this.updatetime();
            }
            return;
        }// end function

        protected function mouseOut(event:MouseEvent) : void
        {
            (event.currentTarget as Label).color = 5485622;
            return;
        }// end function

        protected function onOver(event:MouseEvent) : void
        {
            (event.currentTarget as Label).color = 14786337;
            return;
        }// end function

        protected function gotoBuy(event:MouseEvent) : void
        {
            DialogMgr.instance.open(AppDialog.SC_BUY, {storeId:7});
            return;
        }// end function

        protected function onclose(event:MouseEvent) : void
        {
            this.close();
            return;
        }// end function

        public function close() : void
        {
            ModelProxy.change("plugin", "close", "everyday");
            return;
        }// end function

        public function closeComplete() : void
        {
            if (this._timeIntvalId > 0)
            {
                SecondTimerUtil.instance.removeTask(this._timeIntvalId);
                this._timeIntvalId = 0;
            }
            return;
        }// end function

        private function dshdView() : void
        {
            var _loc_2:String = null;
            var _loc_3:Shape = null;
            var _loc_4:Object = null;
            while (this.sp2.numChildren > 0)
            {
                
                this.sp2.removeChildAt(0);
            }
            var _loc_1:int = 0;
            while (_loc_1 < 5)
            {
                
                _loc_3 = new Shape();
                _loc_3.graphics.beginFill(9474192, 0.1);
                _loc_3.graphics.drawRect(3, 100 + _loc_1 * 56, 500, 28);
                _loc_3.graphics.endFill();
                this.sp2.addChild(_loc_3);
                _loc_1++;
            }
            if (this.gameactive == null)
            {
                this.gameactive = ConfigDictionary.data.gameactive;
            }
            this.listarr = [];
            for (_loc_2 in this.gameactive)
            {
                
                if (this.gameactive[_loc_2])
                {
                    _loc_4 = new Object();
                    _loc_4.data = this.gameactive[_loc_2];
                    _loc_4.sort = this.gameactive[_loc_2].sort;
                    _loc_4.sort2 = 0;
                    this.listarr.push(_loc_4);
                }
            }
            this.hefu = ModelProxy.getValue("data", "hefu");
            this.hefuDay = ModelProxy.getValue("data", "hefuday");
            btnfly.addEventListener(MouseEvent.CLICK, this.triggeredHanlder);
            list.mouseHandler = new Handler(this.hdchickHandler);
            list.renderHandler = new Handler(this.hdrmRender);
            list.array = this.listarr;
            return;
        }// end function

        public function gotofind(param1:Object) : void
        {
            if (param1 == null)
            {
                return;
            }
            var _loc_2:* = param1.split("#");
            ModelProxy.change("data", "taskfindpath", _loc_2[0]);
            ModelProxy.change("zone", "stopGuaji", null);
            ModelProxy.change("zone", "autoMove", {x:_loc_2[1], y:_loc_2[2], tostageid:_loc_2[3], onhorse:true});
            return;
        }// end function

        protected function linkGoHandler(event:TextEvent) : void
        {
            var _loc_2:String = null;
            if (this._curdata.state == 1)
            {
                _loc_2 = event.text;
                this.gotofind(_loc_2);
            }
            else
            {
                ModelProxy.change("movetips", "error", "活动尚未开启，禁止传送");
            }
            return;
        }// end function

        protected function goHandler(event:Event) : void
        {
            var mapid:int;
            var npcid:String;
            var e:* = event;
            var label:* = e.currentTarget as Label;
            mapid = int(label.tag.data.mapid);
            npcid = label.tag.data.npcid;
            Alert2.show("即将传送，是否确认前往！", "温馨提示", ["确定", "取消"], function (param1:int, param2:Object) : void
            {
                if (param1 == 0)
                {
                    if (mapid > 0)
                    {
                        PluginManager.instance.sendToServer("instance", CMDInstance.HuoDongApp, mapid);
                    }
                    else
                    {
                        fastgotofind(npcid);
                    }
                }
                return;
            }// end function
            );
            return;
        }// end function

        private function hdchickHandler(event:MouseEvent, param2:int) : void
        {
            var _loc_3:Image = null;
            var _loc_4:Image = null;
            if (param2 < 0)
            {
                return;
            }
            if (event.type == MouseEvent.CLICK)
            {
                _loc_3 = list.getCell(param2).getChildByName("fag") as Image;
                _loc_3.visible = false;
                this.rightChange(param2);
            }
            else if (event.type == MouseEvent.ROLL_OVER)
            {
                _loc_4 = list.getCell(param2).getChildByName("blueline2") as Image;
                _loc_3 = list.getCell(param2).getChildByName("fag") as Image;
                if (_loc_4.filters.length > 0)
                {
                    _loc_3.visible = false;
                }
                else
                {
                    _loc_3.visible = true;
                }
            }
            else if (event.type == MouseEvent.ROLL_OUT)
            {
                _loc_3 = list.getCell(param2).getChildByName("fag") as Image;
                _loc_3.visible = false;
            }
            return;
        }// end function

        private function itemRender(param1:Component, param2:int) : void
        {
            var _loc_3:FlashItem = null;
            if (param2 < itemlist.length)
            {
                _loc_3 = param1.getChildByName("fi") as FlashItem;
                _loc_3.effshow = true;
                _loc_3.data = itemlist.array[param2];
            }
            return;
        }// end function

        private function hdrmRender(param1:Component, param2:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:Label = null;
            var _loc_5:Label = null;
            var _loc_6:Label = null;
            var _loc_7:Label = null;
            var _loc_8:Image = null;
            var _loc_9:String = null;
            var _loc_10:Array = null;
            var _loc_11:Array = null;
            var _loc_12:Array = null;
            var _loc_13:int = 0;
            var _loc_14:Date = null;
            var _loc_15:Date = null;
            var _loc_16:Date = null;
            var _loc_17:String = null;
            var _loc_18:String = null;
            var _loc_19:Boolean = false;
            var _loc_20:Boolean = false;
            var _loc_21:int = 0;
            var _loc_22:Array = null;
            var _loc_23:Array = null;
            var _loc_24:Array = null;
            if (param2 < list.length)
            {
                _loc_3 = list.array[param2];
                _loc_4 = param1.getChildByName("txt1") as Label;
                _loc_5 = param1.getChildByName("txt2") as Label;
                _loc_6 = param1.getChildByName("txt3") as Label;
                _loc_7 = param1.getChildByName("btnfly") as Label;
                _loc_7.underline = false;
                var _loc_25:int = 16775087;
                _loc_6.color = 16775087;
                _loc_4.color = _loc_25;
                _loc_8 = param1.getChildByName("blueline2") as Image;
                _loc_8.url = "png.common.cs2_selectbg" + (param2 % 2 + 1);
                _loc_4.text = _loc_3.data.name;
                _loc_6.text = _loc_3.data.level;
                if (_loc_3.data.level > UserData.getUserData("level"))
                {
                    _loc_6.color = 16711680;
                }
                else
                {
                    _loc_6.color = 65280;
                }
                _loc_9 = _loc_3.data.time;
                _loc_10 = _loc_9.split("-");
                _loc_11 = (_loc_10[0] as String).split("#");
                _loc_12 = (_loc_10[1] as String).split("#");
                _loc_13 = int((ModelProxy.getValue("data", "servertime") + Number(getTimer())) / 1000);
                _loc_14 = new Date(_loc_13 * 1000);
                _loc_15 = new Date(_loc_14.fullYear, _loc_14.month, _loc_14.date, int(_loc_11[0]), int(_loc_11[1]), 0, 0);
                _loc_16 = new Date(_loc_14.fullYear, _loc_14.month, _loc_14.date, int(_loc_12[0]), int(_loc_12[1]), 0, 0);
                if (_loc_15.time > _loc_16.time)
                {
                    _loc_16.time = _loc_16.time + 86400000;
                }
                _loc_17 = "" + _loc_14.day;
                _loc_18 = _loc_3.data.week;
                _loc_19 = true;
                _loc_20 = false;
                if (_loc_3.data.hf)
                {
                    if (this.hefu && this.hefuDay <= 7)
                    {
                        _loc_20 = true;
                        if (!_loc_3.data.hf[this.hefuDay])
                        {
                            _loc_19 = false;
                        }
                    }
                }
                if (!_loc_20)
                {
                    if (_loc_18)
                    {
                        _loc_21 = ModelProxy.getValue("data", "openday");
                        _loc_22 = _loc_18.split("&");
                        _loc_23 = (_loc_22[0] as String).split("*");
                        _loc_24 = (_loc_22[1] as String).split("#");
                        if (_loc_21 <= 4 && int(_loc_23[0]) > 0)
                        {
                            if (_loc_23.indexOf(((_loc_21 + 1)).toString()) != -1)
                            {
                                _loc_19 = true;
                            }
                            else
                            {
                                _loc_19 = false;
                            }
                        }
                        else if (_loc_24.indexOf(_loc_17) != -1)
                        {
                            _loc_19 = true;
                        }
                        else
                        {
                            _loc_19 = false;
                        }
                    }
                }
                if (_loc_13 >= _loc_15.time / 1000 && _loc_13 <= _loc_16.time / 1000)
                {
                    if (_loc_19)
                    {
                        if (_loc_5.text != "活动开放中")
                        {
                            _loc_7.color = MColor.T_GREEN;
                            _loc_3.data.state = 1;
                            _loc_5.text = "活动开放中";
                            _loc_7.tag = param1.dataSource;
                            _loc_4.color = MColor.V_NORMAL2;
                            _loc_5.color = MColor.T_GREEN2;
                            _loc_7.text = "前往";
                            if (!_loc_7.hasEventListener(MouseEvent.CLICK))
                            {
                                _loc_7.addEventListener(MouseEvent.CLICK, this.goHandler);
                            }
                            _loc_7.addEventListener(MouseEvent.ROLL_OVER, this.overTxt5);
                            _loc_7.addEventListener(MouseEvent.ROLL_OUT, this.outTxt5);
                            _loc_3.sort2 = 1;
                        }
                    }
                    else
                    {
                        var _loc_25:* = MColor.V_NDOWN2;
                        _loc_4.color = MColor.V_NDOWN2;
                        var _loc_25:* = _loc_25;
                        _loc_5.color = _loc_25;
                        var _loc_25:* = _loc_25;
                        _loc_6.color = _loc_25;
                        _loc_7.color = _loc_25;
                        _loc_7.text = START;
                        _loc_5.text = "今天不开放";
                        _loc_3.sort2 = 6;
                    }
                }
                else if (_loc_13 < _loc_15.time / 1000)
                {
                    if (_loc_19)
                    {
                        _loc_7.text = START;
                        _loc_7.color = "0xff0000";
                        _loc_3.data.state = 0;
                        _loc_5.color = MColor.T_RED;
                        _loc_3.sort2 = 2;
                    }
                    else
                    {
                        var _loc_25:* = MColor.V_NDOWN2;
                        _loc_4.color = MColor.V_NDOWN2;
                        var _loc_25:* = _loc_25;
                        _loc_5.color = _loc_25;
                        var _loc_25:* = _loc_25;
                        _loc_6.color = _loc_25;
                        _loc_7.color = _loc_25;
                        _loc_7.text = START;
                        _loc_3.sort2 = 5;
                    }
                    _loc_5.text = _loc_9.replace(/#""#/g, ":");
                }
                else
                {
                    if (_loc_19)
                    {
                        _loc_3.data.state = 0;
                        _loc_7.text = END;
                        _loc_3.sort2 = 3;
                        _loc_4.color = MColor.V_NDOWN2;
                        _loc_5.color = MColor.V_NDOWN2;
                        _loc_6.color = MColor.V_NDOWN2;
                        _loc_7.color = MColor.V_NDOWN2;
                    }
                    else
                    {
                        var _loc_25:* = MColor.V_NDOWN2;
                        _loc_4.color = MColor.V_NDOWN2;
                        var _loc_25:* = _loc_25;
                        _loc_5.color = _loc_25;
                        var _loc_25:* = _loc_25;
                        _loc_6.color = _loc_25;
                        _loc_7.color = _loc_25;
                        _loc_7.text = START;
                        _loc_3.sort2 = 4;
                    }
                    _loc_5.text = _loc_9.replace(/#""#/g, ":");
                }
                if (this._timeIntvalId == 0)
                {
                    this._timeIntvalId = SecondTimerUtil.instance.addSecondTask(this.updatetime);
                }
            }
            return;
        }// end function

        protected function outTxt5(event:MouseEvent) : void
        {
            var _loc_2:* = event.currentTarget as Label;
            if (_loc_2.text == "前往")
            {
                _loc_2.color = MColor.T_GREEN;
            }
            return;
        }// end function

        protected function overTxt5(event:MouseEvent) : void
        {
            var _loc_2:* = event.currentTarget as Label;
            if (_loc_2.text == "前往")
            {
                _loc_2.color = "0xe19f21";
            }
            return;
        }// end function

        private function updatetime() : void
        {
            this.listarr.sortOn(["sort2", "sort"], [Array.NUMERIC, Array.NUMERIC]);
            list.array = this.listarr;
            if (this.isflag)
            {
                this.rightChange(0);
                this.isflag = false;
            }
            return;
        }// end function

        private function rightChange(param1:int) : void
        {
            var _loc_3:Box = null;
            var _loc_12:Array = null;
            var _loc_13:int = 0;
            var _loc_14:Object = null;
            var _loc_2:Boolean = true;
            for each (_loc_3 in list.cells)
            {
                
                this.blueline = _loc_3.getChildByName("blueline2") as Image;
                if (this.blueline)
                {
                    this.blueline.filters = null;
                }
            }
            if (param1 > 13 || param1 < 2)
            {
                list.scrollTo(param1);
            }
            list.selectedIndex = param1;
            var _loc_4:* = list.getCell(param1);
            this.blueline = _loc_4.getChildByName("blueline2") as Image;
            if (this.blueline)
            {
                this.blueline.filters = [new GlowFilter(16750597, 0.5, 6, 6, 2, 1, true)];
            }
            var _loc_5:* = list.array[param1].data;
            this._curdata = _loc_5;
            if (_loc_5.npcname != 0)
            {
                btnfly.visible = true;
                npcName.text = "<a href=\'event:" + _loc_5.npcid + "\'><u>" + _loc_5.npcname + "</u></a>";
                npcName.addEventListener(MouseEvent.MOUSE_OVER, this.onOver);
                npcName.addEventListener(MouseEvent.MOUSE_OUT, this.mouseOut);
                npctxt.visible = true;
                npcName.addEventListener(TextEvent.LINK, this.linkGoHandler);
            }
            else
            {
                npcName.text = "";
                npctxt.visible = false;
                btnfly.visible = false;
            }
            if (_loc_5.level > UserData.getUserData("level"))
            {
                tiaojiantxt.text = MColor.getHMTL(_loc_5.level + "级", 9);
            }
            else
            {
                tiaojiantxt.text = MColor.getHMTL(_loc_5.level + "级", 7);
            }
            var _loc_6:* = _loc_5.time;
            var _loc_7:* = _loc_5.time.split("-");
            var _loc_8:* = (_loc_5.time.split("-")[0] as String).split("#");
            var _loc_9:* = (_loc_7[1] as String).split("#");
            timetxt.text = MColor.getHMTL(_loc_8[0] + ":" + _loc_8[1] + "-" + _loc_9[0] + ":" + _loc_9[1], 5);
            var _loc_10:* = ModelProxy.getValue("data", "hefu");
            jieshaotxt.text = MColor.getHMTL(_loc_10 ? (_loc_5.info1) : (_loc_5.info), 15);
            if (this.cfgtools == null)
            {
                this.cfgtools = ConfigDictionary.data.item;
            }
            var _loc_11:* = _loc_5.items;
            this.arr3 = [];
            if (_loc_11)
            {
                _loc_12 = _loc_11.split("#");
                _loc_13 = 0;
                while (_loc_13 < _loc_12.length)
                {
                    
                    _loc_14 = this.cfgtools[_loc_12[_loc_13]];
                    this.arr3.push(_loc_14);
                    _loc_13++;
                }
            }
            itemlist.array = this.arr3;
            itemlist.renderHandler = new Handler(this.itemRender);
            return;
        }// end function

        protected function triggeredHanlder(event:MouseEvent) : void
        {
            if (this._curdata.state == 1)
            {
                this.fastgotofind(this._curdata.npcid);
            }
            else
            {
                ModelProxy.change("movetips", "error", "活动尚未开启，禁止传送");
            }
            return;
        }// end function

        public function fastgotofind(param1:Object) : void
        {
            if (param1 == null)
            {
                return;
            }
            var _loc_2:* = param1.split("#");
            if (_loc_2[0] > 0)
            {
                ModelProxy.change("data", "taskfindpath", _loc_2[0]);
            }
            ModelProxy.change("zone", "stopGuaji", null);
            PluginManager.instance.sendToServer("activity", CMDActivity.Trans, {x:_loc_2[1], y:_loc_2[2], tostageid:_loc_2[3], free:1});
            ModelProxy.change("zone", "autoMove", {x:_loc_2[1], y:_loc_2[2], tostageid:_loc_2[3], onhorse:true});
            return;
        }// end function

    }
}
